//
//  TodoEntity.swift
//  memo
//
//  Created by DaleGuo on 2023/10/4.
//

import Foundation
import CoreData

extension TodoEntity {
    public var id: String {
        // 只在第一次生成 没有setter方法。见awakeFromInsert
        #if DEBUG
        id_!
        #else
        id_ ?? UUID().uuidString
        #endif
    }
    
    convenience init(title: String,
                     date: Date,
                     context: NSManagedObjectContext) {
        // 调用init
        self.init(context: context) // 配置了context后
        self.content = content
        self.updated_date = date
    }
    
    
    
    var content: String {
        
        // getter setter
        get { content_ ?? ""  }
        set { content_ = newValue }
    }
    
    var updated_date: Date {
        get { updated_date_ ?? Date() }
        set { updated_date_ = newValue }
    }
    
    // 每次创建项目都会调用的，（生命周期）复写
    public override func awakeFromInsert() {
        self.id_ = UUID().uuidString
    }
}

extension TodoEntity: Comparable {
    public static func < (lhs: TodoEntity, rhs: TodoEntity) -> Bool {
        lhs.content < rhs.content
    }
    
    
}
